﻿using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Tools.Ribbon;
using Application=Microsoft.Office.Interop.Excel.Application;

namespace ExcelAddInStudy
{
	public partial class Ribbon : OfficeRibbon
	{
		private ListObject table;

		public Ribbon()
		{
			InitializeComponent();
		}

		private void Ribbon_Load(object sender, RibbonUIEventArgs e)
		{

		}

		private void CreateTable(object sender, RibbonControlEventArgs e)
		{
			var sheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
			if (null == sheet) return;

			var range = Globals.ThisAddIn.Application.Selection as Range;
			if (null == range) return;

			table = sheet.ListObjects.Add(XlListObjectSourceType.xlSrcRange, range, null, XlYesNoGuess.xlYes, null);
		}

		private void SelectTable(object sender, RibbonControlEventArgs e)
		{
			var sheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
			if (null == sheet) return;

			var range = Globals.ThisAddIn.Application.Selection as Range;
			if (null == range) return;

			var name = (string)range.Value2;

			table = sheet.ListObjects[name];

			//foreach (ListObject lo in sheet.ListObjects)
			//{
			//    if (null == Globals.ThisAddIn.Application.Intersect(
			//        lo.Range, range, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
			//        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
			//        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
			//        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
			//        Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
			//        Missing.Value, Missing.Value, Missing.Value, Missing.Value)) continue;
			//    table = lo;
			//    break;
			//}
			//MessageBox.Show("No table selected");
		}

		private void AddRow(object sender, RibbonControlEventArgs e)
		{
			if (null == table || !table.Active) return;

			var sheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
			if (null == sheet) return;

			if (0 != table.ListRows.Count)
			{
				var lastRow = (Range)table.Range.Worksheet.Cells[table.ListRows[table.ListRows.Count].Range.Row + 1, 1];
				lastRow.EntireRow.Insert(XlInsertShiftDirection.xlShiftDown, Missing.Value);
				var resize = table.Range.get_Resize(table.Range.Rows.Count + 1, table.Range.Columns.Count);
				table.Resize(resize);
			}
			else
			{
				var topleft = (Range)sheet.Cells[table.HeaderRowRange.Row + 1, table.HeaderRowRange.Column];
				topleft.Value2 = 1;

				MessageBox.Show(table.ListRows.Count.ToString());
			}

			// MessageBox.Show(lastRow.Row.ToString());

			var row = table.ListRows[table.ListRows.Count];

			var values = new string[table.ListColumns.Count];

			for (int i = 0; i < table.ListColumns.Count; i++)
			{
				values[i] = string.Format("{0} - {1}", i, table.ListRows.Count);
			}
			row.Range.Value2 = values;
		}

		private void ClearTable(object sender, RibbonControlEventArgs e)
		{
			if (null == table) return;
			while (table.ListRows.Count > 0)
			{
				table.ListRows[table.ListRows.Count].Delete();
			}
		}

		private void SelectRange(object sender, RibbonControlEventArgs e)
		{
			var sheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
			if (null == sheet) return;

			var range = Globals.ThisAddIn.Application.Selection as Range;
			if (null == range) return;

			Range resized = range.get_Resize(range.Rows.Count + 1, range.Columns.Count + 1);

			resized.Select();
		}

		private void EnlargeTable(object sender, RibbonControlEventArgs e)
		{
			var sheet = (Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
			if (null == sheet) return;

			if (null == table || !table.Active) return;

			var resize = table.Range.get_Resize(table.Range.Rows.Count + 1, table.Range.Columns.Count);
			table.Resize(resize);

			((Range) sheet.Cells[1, 1]).Value2 = table.ListRows.Count;
		}

		private void AddContextMenu(object sender, RibbonControlEventArgs e)
		{
		    AddMenu();
		}

		private void AddMenu()
		{
			var button = (CommandBarButton)Application.CommandBars["List Range Popup"].Controls.Add(
				MsoControlType.msoControlButton, 
				Type.Missing, 
				Type.Missing, 
				Application.CommandBars["List Range Popup"].Controls.Count + 1, 
				true
				);
			button.Caption = "See details";
			button.BeginGroup = true;
			button.TooltipText = "See details";
			button.OnAction = "DrillDownToLine";
			button.Tag = "SeeDetail";
		}

		private void RemoveMenu()
		{
			var bar = Application.CommandBars["List Range Popup"];
			var button = (CommandBarButton)bar.FindControl(
				MsoControlType.msoControlButton, 0, "SeeDetail", Type.Missing, Type.Missing
				);
			if (button == null)
				bar.Reset();
			else
			{
				button.Delete(Type.Missing);
			}
		}

		private static Application Application
		{
			get { return Globals.ThisAddIn.Application; }
		}
	}
}
